package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Product;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.ProductService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.UUID;

public class ProductServiceImpl implements ProductService {
    //查询数据
    @Override
    public List<Product> findAll(String pageNum, String pageSize) {
        //根据工厂化对象获取session的对象
        SqlSession session = MapperFactory.getSqlSession();

        //根据session对象获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);

        //对获取的数据进行数据类型处理
        int pageN=1;
        int pageS=3;
        if(StringUtils.isNoneBlank(pageNum)){
            pageN= Integer.parseInt(pageNum);
        }

        if(StringUtils.isNoneBlank(pageSize)){
            pageS=Integer.parseInt(pageSize);
        }

        //设置分页参数:当前页码，每页显示的条目数
        PageHelper.startPage(pageN,pageS);

        //调用接口实现类的方法
        List<Product> list = mapper.findAll();

        //释放资源
        TransactionUtil.close(session);
        return list;
    }

    //存储数据
    @Override
    public void save(Product product) {
        //调用工具类方法获取session对象
        SqlSession session = MapperFactory.getSqlSession();
        //通过session获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //调用接口实现类中的方法
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        product.setId(id);
        //调用实现类的方法
        mapper.save(product);
        //事务提交
        TransactionUtil.commit(session);
        //释放资源
        TransactionUtil.close(session);
    }

    //删除数据
    @Override
    public void deleteByIds(String [] id) {
        //通过工具类获取session对象
        SqlSession session = MapperFactory.getSqlSession();

        //通过session对象获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);

        //调用接口实现类的方法
        mapper.deleteByIds(id);

        //事务提交
        TransactionUtil.commit(session);

        //释放资源
        TransactionUtil.close(session);
    }


    //修改数据前数据回显
    @Override
    public Product findById(String id) {
        //通过工具类获取session对象
        SqlSession session = MapperFactory.getSqlSession();
        //通过session获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //调用接口的方法
        Product product=mapper.findById(id);
        //事务提交
        TransactionUtil.commit(session);
        //释放资源
        TransactionUtil.close(session);

        return product;
    }

    //修改数据
    @Override
    public void update(Product product) {
        //根据工厂化对象获取session的对象
        SqlSession session = MapperFactory.getSqlSession();

        //根据session对象获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);

        product.setProductStatusStr(String.valueOf(product.getProductStatus()));


        //调用接口实现类的方法
        mapper.update(product);

        //事务提交
        TransactionUtil.commit(session);
        //释放资源
        TransactionUtil.close(session);
    }

    @Override
    public void openByIds(String [] id) {
        //通过工具类获取session对象
        SqlSession session = MapperFactory.getSqlSession();
        //通过session获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //调用接口的方法
        mapper.openByIds(id);
        //事务提交
        TransactionUtil.commit(session);
        //释放资源
        TransactionUtil.close(session);

    }

    @Override
    public void closeByIds(String [] id) {
        //通过工具类获取session对象
        SqlSession session = MapperFactory.getSqlSession();
        //通过session获取接口的实现类对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //调用接口的方法
        mapper.closeByIds(id);
        //事务提交
        TransactionUtil.commit(session);
        //释放资源
        TransactionUtil.close(session);

    }

    @Override
    public ByteArrayOutputStream download() throws IOException {
        SqlSession session = MapperFactory.getSqlSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //获取对应要展示的数据
        List<Product> productList=mapper.findAll();

        //获取到对应的Excel文件
            XSSFWorkbook wb = new XSSFWorkbook();

            //创建工作表
            Sheet s = wb.createSheet("产品数据文件");

            //设置通用格式
            s.setColumnWidth(4,500);
            CellStyle cs_field = wb.createCellStyle();
            cs_field.setAlignment(HorizontalAlignment.CENTER);
            cs_field.setBorderTop(BorderStyle.THIN);
            cs_field.setBorderBottom(BorderStyle.THIN);
            cs_field.setBorderLeft(BorderStyle.THIN);
            cs_field.setBorderRight(BorderStyle.THIN);

            //制作标题
            s.addMergedRegion(new CellRangeAddress(1,1,1,7));

            Row row1 = s.createRow(1);
            Cell cell_1_1 = row1.createCell(1);
            cell_1_1.setCellValue("在线产品导出信息");
            //创建样式
            CellStyle cs_title = wb.createCellStyle();
            cs_title.setAlignment(HorizontalAlignment.CENTER);
            cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
            cell_1_1.setCellStyle(cs_title);

            //制作表头
            String [] fields ={"id值","产品编号","产品名称","出发城市","出发时间","产品价格","产品描述","产品状态"};

            Row row_2 = s.createRow(2);

            for(int i=0;i<fields.length;i++) {
                Cell cell_2_temp = row_2.createCell(1);
                cell_2_temp.setCellValue(fields[0]);
                cell_2_temp.setCellStyle(cs_field);
            }

            //制作数据区

            int row_index=0;

            for(Product p:productList) {
                int cell_index=0;
                Row row_temp = s.createRow(3+row_index++);
                Cell cell_data_1 = row_temp.createCell(1 + cell_index++);
                cell_data_1.setCellValue(p.getId());
                cell_data_1.setCellStyle(cs_field);

                Cell cell_data_2 = row_temp.createCell(1 + cell_index++);
                cell_data_2.setCellValue(p.getProductNum());
                cell_data_2.setCellStyle(cs_field);

                Cell cell_data_3 = row_temp.createCell(1 + cell_index++);
                cell_data_3.setCellValue(p.getProductName());
                cell_data_3.setCellStyle(cs_field);

                Cell cell_data_4 = row_temp.createCell(1 + cell_index++);
                cell_data_4.setCellValue(p.getCityName());
                cell_data_4.setCellStyle(cs_field);

                Cell cell_data_5 = row_temp.createCell(1 + cell_index++);
                cell_data_5.setCellValue(p.getDepartureTime());
                cell_data_5.setCellStyle(cs_field);

                Cell cell_data_6 = row_temp.createCell(1 + cell_index++);
                cell_data_6.setCellValue(p.getProductPrice());
                cell_data_6.setCellStyle(cs_field);

                Cell cell_data_7 = row_temp.createCell(1 + cell_index++);
                cell_data_7.setCellValue(p.getProductDesc());
                cell_data_7.setCellStyle(cs_field);


                Cell cell_data_8 = row_temp.createCell(1 + cell_index++);
                cell_data_8.setCellValue(p.getProductStatus());
                cell_data_8.setCellStyle(cs_field);
            }

            //创建一个文件对象，作为excel文件内容的输出文件

            //输出时通过流的形式对外输出，包装对应的目标文件
        ByteArrayOutputStream os=new ByteArrayOutputStream();
            wb.write(os);
            wb.close();
            return os;
    }
}
